package com.weixing.mall.provider.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.google.common.collect.Lists;
import com.weixing.mall.core.support.auth.AuthUtil;
import com.weixing.mall.core.support.auth.MemAuth;
import com.weixing.mall.provider.api.service.AgentServiceRpc;
import com.weixing.mall.provider.mapper.MemberMessageMapper;
import com.weixing.mall.provider.mapper.MessageTypeMapper;
import com.weixing.mall.provider.model.domain.MemberMessage;
import com.weixing.mall.provider.model.domain.MessageType;
import com.weixing.mall.provider.model.dto.MemberMessageDto;
import com.weixing.mall.provider.model.enums.MemberMsgTypeEnum;
import com.weixing.mall.provider.model.query.MemberMessageQuery;
import com.weixing.mall.provider.model.vo.AdminMemberMessageVo;
import com.weixing.mall.provider.model.vo.FrontMemberMessageVo;
import com.weixing.mall.provider.model.vo.MemberMessageVo;
import com.weixing.mall.provider.service.IMemberMessageService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 会员模块-会员消息表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-13
 */
@Service
public class MemberMessageServiceImpl extends ServiceImpl<MemberMessageMapper, MemberMessage> implements IMemberMessageService {
    @Autowired
    private MessageTypeMapper messageTypeMapper;

    @Autowired
    private AgentServiceRpc agentServiceRpc;
    @Override
    public MemberMessageVo selectDetail(Long id) {
        return baseMapper.selectDetail(id);
    }

    @Override
    public IPage selectByMemberId(Page page, long memberId) {
        return baseMapper.selectByMemberId(page,memberId);
    }

    @Override
    public int noReadMessage(Long mid) {
        return baseMapper.noReadMessage(mid);

    }

    @Override
    public List<MemberMessageVo> noReadMessageByType(Long mid) {
        return baseMapper.noReadMessageByType(mid);
    }

    @Override
    public List<MemberMessageVo> selectMessageByType(Long mid, Long typeId) {
        MemberMessageQuery entity=new MemberMessageQuery();
        entity.setMemberId(mid);
        entity.setMsgType(typeId);
        return baseMapper.selectMessageByType(entity);
    }
    /*TODO 和CustomerMessageServiceImpl存在冲突，暂时使用它们的。*/
    @Override
    @Transactional
    public void saveBit(MemberMessageDto memberMessageDto) {
        int msgtype=memberMessageDto.getMsgType();
      MessageType messageType= messageTypeMapper.selectById(msgtype);
      String typeRange=messageType.getTypeRange();
      String [] typeRangArray=null==typeRange?new String[0]:typeRange.split(",");
      List<Long > tt=new ArrayList<Long>();
      for(int i=0;i<typeRangArray.length;i++){
          int type=Integer.valueOf(typeRangArray[i]);
          if(type!=0){
              tt.addAll(agentServiceRpc.selectAgentIdByAgentLevel(type));
          }else {
             /* baseMapper.selectList()*/
          }

      }
        save(new MemberMessage());


    }

    @Override
    public void msgReply(MemberMessageDto memberMessageDto) {
        if(memberMessageDto != null && memberMessageDto.getId() != null){
            MemberMessage memberMessage = new MemberMessage();
            memberMessage.setId(memberMessageDto.getId());
            memberMessage.setReply(memberMessageDto.getReply());
            updateById(memberMessage);
        }
    }

    @Override
    public IPage<AdminMemberMessageVo> selectMemberMessageList(Page page, MemberMessageQuery memberMessageQuery) {
        return baseMapper.selectMemberMessageList(page,memberMessageQuery);
    }

    @Override
    public IPage<FrontMemberMessageVo> selectMemberMsgList(Page page, Integer type) {
        MemAuth auth = AuthUtil.getMemberExt();
        LambdaQueryWrapper<MemberMessage> qw = new LambdaQueryWrapper<>();
        if(MemberMsgTypeEnum.TYPE2.getValue().equals(type)){//若是为代理商 将其私有消息和公共消息共同查出
           qw.eq(MemberMessage::getAgentId,auth.getAbid()).eq(MemberMessage::getMsgType,MemberMsgTypeEnum.TYPE2.getValue()).or().eq(MemberMessage::getMsgType,MemberMsgTypeEnum.TYPE1.getValue());
        }else{//若只是会员  只查询公共消息
            qw.eq(MemberMessage::getMsgType,MemberMsgTypeEnum.TYPE1.getValue());
        }
        qw.orderByDesc(MemberMessage::getCreatedTime);
        IPage<MemberMessage> pageList = page(page,qw);
        List<FrontMemberMessageVo> list = Lists.newArrayList();
        if( pageList.getRecords().size() > 0){
            pageList.getRecords().forEach(memberMessage -> {
                FrontMemberMessageVo fv = new FrontMemberMessageVo();
                BeanUtils.copyProperties(memberMessage,fv);
                list.add(fv);
            });
        }
        return new Page<FrontMemberMessageVo>().setSize(page.getSize()).setCurrent(page.getCurrent()).setTotal(page.getTotal()).setRecords(list);
    }
}
